/**
 * 功能描述 
 */
define(function (require, exports, module){
	var EditGrid=require("editgrid");
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");
	
	var instance = {
			initComponent: function (params,widget){
				var damagesConsumpEdit = new EditGrid({
					parentNode:".J-consumpeditgrid",
					autoRender:false,
					model:{
						id:"damagesConsumpEdit",
						head: {
							title: "物品赔偿"
						},
						onAdd: function(editors) {
							var goodsName = editors["goods.name"].getValue();
							var num = editors["num"].getValue();
							var description = editors["description"].getValue();
							if(!description){
								description = widget.get("roomCode") + "-的物品赔偿";
							}
							var price = editors["goods.name"].getData(goodsName).price;
							if(!goodsName || !num){
								return false;
							}else{
								var reg = /^[0-9]*[1-9][0-9]*$/;
								if(isNaN(num) || !reg.test(num)){
									Dialog.alert({
										tip: "提示！",
										content: "数量只能输入正整数！"
									});
									return false;
								}
								damagesConsumpEdit.add({
									goods: editors["goods.name"].getData(goodsName).goods,
									price: price,
									num: num,
									consumptionType: "DamageConsumption",
									sum: parseFloat(price)*parseFloat(num),
									description: description,
									version:0
								});
								return true;
							}
						},
						columns:[{
							name:"goods.name",
							label:"物品名称",
							editor:{
								type:"select",
								url:"api/serpointsertyperelation/query",
								params:function(){
									var obj={
										type:"Goods",
										"goods.goodsType.roomAssets":"true",
			    	     				fetchProperties:"goods.pkGoods,goods.name,price"
			                		}
			                		return obj;
								},
								keyField:"goods.pkGoods",
								valueField:"goods.name",
								onEdit : function(editor,rowIndex,rowData){
									editor.setValue(rowData.goods);
								},
								onChange : function(plugin,index,rowData){
									rowData.goods = plugin.getData(plugin.getValue());
									damagesConsumpEdit.update(index,rowData);
								}
							}
						},{
							name:"price",
							label:"单价",
							format: function(value){
								return parseFloat(value).toFixed(2);
							}
						},{
							name:"num",
							label:"数量",
							editor:{
								type:"text",
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.num);
								},
								onChange:function(plugin,index,rowData){
									rowData.num = plugin.getValue();
									damagesConsumpEdit.update(index,rowData);
								}
							}
						},{
							name:"sum",
							label:"总金额",
							format: function(value, row){
								if(value){
									return parseFloat(value).toFixed(2);
								}else{
									return parseFloat(row.price*row.num).toFixed(2);
								}
							}
						},{
							name:"description",
							label:"备注",
							editor:{
								type:"text",
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.description);
								},
								onChange:function(plugin,index,rowData){
									rowData.description = plugin.getValue();
									damagesConsumpEdit.update(index,rowData);
								}
							},
						},{
							name:"operate",
							label:"操作",
							format:"button",
							formatparams:[{
								id:"del",
								icon:"icon-remove",
								handler:function(index,data,rowEle){
									damagesConsumpEdit.remove(rowEle);
	  							}
							}]
						}]
					}
				});
				widget.set("damagesConsumpEdit",damagesConsumpEdit);
			}
	};
	module.exports = instance;
});
